<template>
  <div class='sub-container'>
    <div class="container">
      <!-- 面包屑导航组件 -->
      <SubBread />
      <!-- 条件筛选组件 -->
      <SubFilter />
      <!-- 复选框测试 -->
      <!-- <XtxCheckbox v-model='job'>程序员</XtxCheckbox> {{job}} -->
      <!-- 商品列表 -->
      <div class="goods-list">
        <!-- 排序组件 -->
        <SubSort />
        <!-- 商品列表 -->
        <ul>
          <li v-for="item in list" :key="item.id">
            <GoodsItem :info="item" />
          </li>
        </ul>
        <!-- 加载更多的组件 -->
        <XtxInfiniteLoading :loading='loading' :finished='finished' @load='getData' />
      </div>
    </div>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import SubBread from './components/sub-bread.vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { findSubCategoryGoods } from '@/api/home.js'

export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem
  },
  setup () {
    // const job = ref(true)
    const route = useRoute()
    const loading = ref(false)
    const finished = ref(false)
    const list = ref([])
    // 查询参数
    const reqParams = {
      page: 1,
      pageSize: 20
    }
    // 调用接口获取下一页数据
    const getData = () => {
      // 接口正在调用
      loading.value = true
      // 请求参数需要添加二级分类id
      reqParams.categoryId = route.params.id
      findSubCategoryGoods(reqParams).then(data => {
        list.value.push(...data.result.items)
        // 获取数据后，重置加载状态
        loading.value = false
        // 页码需要继续累加
        reqParams.page++
        // 是否已经全部加载完成
        if (data.result.counts === list.value.length) {
          // 没有更多数据了
          finished.value = true
        }
      })
    }
    // 侦听二级分类的切换操作
    watch(() => route.params.id, (newId) => {
      // \排除一级分类的参数变化
      if (route.path !== '/category/sub/' + newId) return
      // 清空数据即可
      list.value = []
      // 重新加载第一页数据
      reqParams.page = 1
      // 重置完成的状态
      finished.value = false
    })
    return { getData, loading, finished, list }
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
